package com.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;


@Component
//放在任意一个被spring管理的bean上面即可，不需要与@Value注解放在一起
//只放置一次即可，表示文件只读取一次
//见DefaultResourceLoader类的getResource方法（154行）与getResourceByPath方法
//默认创建的是ClassPathContextResource对象来表示这个文件资源
//DefaultResourceLoader实现了ResourceLoader接口没有实现ResourceLoader的子接口ResourcePatternResolver
//ResourcePatternResolver支持classpath*前缀。
//@PropertySource("db.properties")
//@PropertySource("db2.properties")
public class DbConfig {

    @Autowired
    private Environment env;

    public void testEnv() {
        System.out.println(env.getProperty("jdbc.url"));
        System.out.println(this.url);
    }

    @Value("${jdbc.url:default}")
    private String url;
    private String username;
    private String pwd;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "DbConfig{" +
                "url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }
}
